package com.shelpe.services.sustainer.autoDrive;

import java.util.Date;
import java.util.List;

import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.BeanUtils;

import com.shelpe.services.sustainer.entity.jpa.InprogressAutoDriver;
import com.shelpe.services.sustainer.repository.jpa.InprogressAutoDriverRepository;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class AutoProductWriter implements ItemWriter<AutoProductQueueResult>{

	private InprogressAutoDriverRepository inprogressAutoDriverRepository;
	
	private int step;
	
	@Override
	public void write(List<? extends AutoProductQueueResult> items) throws Exception {
		long campaignID = 0;
		int success = 0;
		int failures = 0;
		int chunk = 0;
		if(items != null && items.size() > 0){
			for(AutoProductQueueResult item: items){
				if(item.getStatus() == ResultStatus.success){
					success++;
				}else if(item.getStatus() != ResultStatus.keep){
					failures++;
				}
			}
		
			for(AutoProductQueueResult res: items){
				campaignID = res.getCampaignID();
				chunk = res.getChunk();
				break;
			}
		}
		
		if(campaignID > 0){
			InprogressAutoDriver.PK pk = new InprogressAutoDriver.PK();
			pk.setCampaignID(campaignID);
			pk.setStep(step);
			pk.setChunk(chunk);
			InprogressAutoDriver driver = this.inprogressAutoDriverRepository.findOne(pk);
			if(driver != null){
				driver.setFailure(driver.getFailure() + failures);
				driver.setSuccess(driver.getSuccess() + success);
				driver.setExecutions(driver.getExecutions() + 1);
				log.debug("driver {} is running in {} round: {} > {} + {}", campaignID, driver.getExecutions(),
						driver.getProducts(), driver.getSuccess(), driver.getFailure());
				if((driver.getSuccess() + driver.getFailure()) >= driver.getProducts()){
					log.debug("driver {} is all done and increment the step: {} -> {}", campaignID, step, step + 1);
					if((driver.getSuccess() + driver.getFailure()) == driver.getProducts()){
						this.createNextStep(driver);
					}else{
						log.error("driver {} is already done, but execute again.", campaignID, driver.getExecutions());
					}
					driver.setDone(1);
				}
				driver.setModifiedTime(new Date());
				this.inprogressAutoDriverRepository.save(driver);
			}else{
				log.error("InprogressAutoDriver not found: {}", pk);
			}
		}
	}
	
	
	private void createNextStep(InprogressAutoDriver driver){
		InprogressAutoDriver newDriver = new InprogressAutoDriver();
		BeanUtils.copyProperties(driver, newDriver);
		newDriver.setStep(step + 1);
		newDriver.setChunk(driver.getChunk());
		newDriver.setFailure(0);
		newDriver.setSuccess(0);
		newDriver.setExecutions(0);
		newDriver.setDone(0);
		newDriver.setCreatedTime(new Date());
		newDriver.setModifiedTime(new Date());
		this.inprogressAutoDriverRepository.save(newDriver);
	}

	public void setInprogressAutoDriverRepository(InprogressAutoDriverRepository inprogressAutoDriverRepository) {
		this.inprogressAutoDriverRepository = inprogressAutoDriverRepository;
	}

	public void setStep(int step) {
		this.step = step;
	}

	
}
